package com.tuanzi.loan.business.repository.sms.impl;

import com.tuanzi.loan.business.constants.sms.SmsVoiceSendTaskStatus;
import com.tuanzi.loan.business.repository.sms.custom.SmsVoiceSendTaskRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.family.sms.SmsVoiceSendTaskSearchRequest;
import com.tuanzi.loan.vo.response.family.sms.SmsVoiceSendTaskSearchResponse;
import org.apache.commons.lang3.StringUtils;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.List;
import java.util.Objects;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanSmsCalculateTask.LOAN_SMS_CALCULATE_TASK;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanSmsVoiceSendTask.LOAN_SMS_VOICE_SEND_TASK;

/**
 * @Author: chenweijun
 * @Date: 2018/10/22 19:57
 * @Description:
 */
public class SmsVoiceSendTaskRepositoryImpl extends JOOQBaseRepository implements SmsVoiceSendTaskRepositoryCustom {

    @Override
    protected TableImpl getTable() {
        return LOAN_SMS_VOICE_SEND_TASK;
    }

    @Override
    public Page<SmsVoiceSendTaskSearchResponse> list(SmsVoiceSendTaskSearchRequest searchRequest, Pageable pageable) {
        fields(LOAN_SMS_CALCULATE_TASK.NAME, LOAN_SMS_VOICE_SEND_TASK.ID, LOAN_SMS_VOICE_SEND_TASK.CONTENT, LOAN_SMS_VOICE_SEND_TASK.VOICE_ID,
                LOAN_SMS_VOICE_SEND_TASK.STATUS, LOAN_SMS_CALCULATE_TASK.PHONE_COUNT, LOAN_SMS_VOICE_SEND_TASK.COST,
                LOAN_SMS_VOICE_SEND_TASK.EXECUTE_TIME, LOAN_SMS_VOICE_SEND_TASK.PLATFORM_CHANNEL, LOAN_SMS_VOICE_SEND_TASK.REGISTER_COUNT,
                LOAN_SMS_VOICE_SEND_TASK.LOAN_COUNT, LOAN_SMS_VOICE_SEND_TASK.CREATE_TIME, LOAN_SMS_VOICE_SEND_TASK.INCOME,
                LOAN_SMS_VOICE_SEND_TASK.REMARK, LOAN_SMS_VOICE_SEND_TASK.SHORT_LINK);

        leftJoin(LOAN_SMS_CALCULATE_TASK, LOAN_SMS_VOICE_SEND_TASK.CALCULATE_TASK_ID.eq(LOAN_SMS_CALCULATE_TASK.ID));

        contains(LOAN_SMS_CALCULATE_TASK.NAME, searchRequest.getName());
        ge(LOAN_SMS_VOICE_SEND_TASK.EXECUTE_TIME, searchRequest.getExecuteTimeStart());
        le(LOAN_SMS_VOICE_SEND_TASK.EXECUTE_TIME, searchRequest.getExecuteTimeEnd());
        ge(LOAN_SMS_CALCULATE_TASK.PHONE_COUNT, searchRequest.getCountStart());
        le(LOAN_SMS_CALCULATE_TASK.PHONE_COUNT, searchRequest.getCountEnd());
        eq(LOAN_SMS_VOICE_SEND_TASK.VOICE_ID, searchRequest.getVoiceId());
        contains(LOAN_SMS_VOICE_SEND_TASK.CONTENT, searchRequest.getContent());
        eq(LOAN_SMS_VOICE_SEND_TASK.STATUS, searchRequest.getStatus());
        eq(LOAN_SMS_VOICE_SEND_TASK.PLATFORM_CHANNEL, searchRequest.getPlatformChannel());
        neq(LOAN_SMS_VOICE_SEND_TASK.STATUS, SmsVoiceSendTaskStatus.WAITING_SUBMIT.ordinal());
        orderBy(LOAN_SMS_VOICE_SEND_TASK.CREATE_TIME, Sort.Direction.DESC);
        return search(pageable, record -> {
            SmsVoiceSendTaskSearchResponse response = new SmsVoiceSendTaskSearchResponse();
            response.setName(record.get(LOAN_SMS_CALCULATE_TASK.NAME));
            response.setId(record.get(LOAN_SMS_VOICE_SEND_TASK.ID));
            response.setVoiceId(record.get(LOAN_SMS_VOICE_SEND_TASK.VOICE_ID));
            response.setContent(record.get(LOAN_SMS_VOICE_SEND_TASK.CONTENT));
            response.setShortLink(record.get(LOAN_SMS_VOICE_SEND_TASK.SHORT_LINK));
            response.setStatus(record.get(LOAN_SMS_VOICE_SEND_TASK.STATUS));
            response.setCost(record.get(LOAN_SMS_VOICE_SEND_TASK.COST) == null ? 0D : record.get(LOAN_SMS_VOICE_SEND_TASK.COST).doubleValue());
            response.setExecuteTime(record.get(LOAN_SMS_VOICE_SEND_TASK.EXECUTE_TIME));
            response.setPlatformChannel(record.get(LOAN_SMS_VOICE_SEND_TASK.PLATFORM_CHANNEL));
            response.setCount(record.get(LOAN_SMS_CALCULATE_TASK.PHONE_COUNT));
            response.setRegisterCount(record.get(LOAN_SMS_VOICE_SEND_TASK.REGISTER_COUNT));
            response.setLoanCount(record.get(LOAN_SMS_VOICE_SEND_TASK.LOAN_COUNT));
            response.setCreateTime(record.get(LOAN_SMS_VOICE_SEND_TASK.CREATE_TIME));
            response.setIncome(Objects.isNull(record.get(LOAN_SMS_VOICE_SEND_TASK.INCOME)) ? null : record.get(LOAN_SMS_VOICE_SEND_TASK.INCOME).floatValue());
            response.setRemark(record.get(LOAN_SMS_VOICE_SEND_TASK.REMARK));
            return response;
        });
    }

    @Override
    public List<String> findDistinctFirstPartyNames() {
        return dslContext.selectDistinct(LOAN_SMS_VOICE_SEND_TASK.FIRST_PARTY_NAME).from(LOAN_SMS_VOICE_SEND_TASK).
                where(LOAN_SMS_VOICE_SEND_TASK.FIRST_PARTY_NAME.isNotNull()).and(LOAN_SMS_VOICE_SEND_TASK.FIRST_PARTY_NAME.notEqual(StringUtils.EMPTY))
                .fetch(LOAN_SMS_VOICE_SEND_TASK.FIRST_PARTY_NAME);
    }

}
